home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 85 / CD Temático 40 Febrero 2004.iso / DOS / testdisk / src / bsd.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-01-09  |  4.8 KB  |  150 lines

  1. /*
  2.  
  3.     File: bsd.c
  4.  
  5.     Copyright (C) 1998-2004 Christophe GRENIER <grenier@cgsecurity.org>
  6.   
  7.     This software is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.   
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.   
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.  */
  22.  
  23. #include <stdio.h>
  24. #include "types.h"
  25. #include "common.h"
  26. #include "bsd.h"
  27. #include "intrface.h"
  28. #include "fnctdsk.h"
  29. int check_BSD(t_param_disk *disk_car,t_diskext *partition,const int debug, const unsigned int max_partitions)
  30. {
  31.   unsigned char buffer[8*SECTOR_SIZE];
  32.   if(disk_car->read(disk_car,8, &buffer, partition->lba))
  33.   { return 1; }
  34.   if(test_BSD(disk_car,(const struct disklabel*)&buffer[0x200],partition,debug,0,max_partitions))
  35.     return 1;
  36.   set_part_name(partition,((const struct disklabel*)(&buffer[0x200]))->d_packname,16);
  37.   if(check_volume_name(partition->name,16))
  38.     partition->name[0]='\0';
  39.   return 0;
  40. }
  41.  
  42. int test_BSD(t_param_disk *disk_car, const struct disklabel*bsd_header,t_diskext *partition,const int debug, const int dump_ind, const unsigned int max_partitions)
  43. {
  44.   if((le32(bsd_header->d_magic) == DISKMAGIC)&&
  45.       (le32(bsd_header->d_magic2)==DISKMAGIC))
  46.   {
  47.     unsigned int i;
  48.     const u_int16_t* cp;
  49.     u_int16_t crc;
  50.     if(debug)
  51.       ecrit_rapport("\nBSD offset %lu, nbr_part %u, CHS=(%u,%u,%u) ",partition->lba,
  52.       bsd_header->d_npartitions,bsd_header->d_ncylinders, bsd_header->d_ntracks, bsd_header->d_nsectors);
  53.     if(bsd_header->d_npartitions>max_partitions)
  54.       return 1;
  55.     crc=0;
  56.     for(cp=(const u_int16_t*)bsd_header;
  57.     cp<(const u_int16_t*)&bsd_header->d_partitions[le16(bsd_header->d_npartitions)];cp++)
  58.       crc^=*cp;
  59.     if(debug)
  60.     {
  61.       if(!crc)
  62.     ecrit_rapport("CRC Ok\n");
  63.       else
  64.     ecrit_rapport("Bad CRC! CRC must be xor'd by %04X\n",crc);
  65.     }
  66.     if(crc)
  67.       return 1;
  68.     for(i=0;i<max_partitions;i++)
  69.     {
  70.       if(bsd_header->d_partitions[i].p_fstype>0)
  71.       {
  72.     if(debug)
  73.     {
  74.       /* UFS UFS2 SWAP */
  75.       ecrit_rapport("BSD %c: ", 'a'+i);
  76.       switch(bsd_header->d_partitions[i].p_fstype)
  77.       {
  78.         case TST_FS_SWAP:
  79.           ecrit_rapport("swap");
  80.           break;
  81.         case TST_FS_BSDFFS:
  82.           ecrit_rapport("4.2BSD fast filesystem");
  83.           break;
  84.         case TST_FS_BSDLFS:
  85.           ecrit_rapport("4.4BSD log-structured filesystem");
  86.           break;
  87.         default:
  88.           ecrit_rapport("type %02X", bsd_header->d_partitions[i].p_fstype);
  89.           break;
  90.       }
  91.       ecrit_rapport(", offset %9d, size %9d ",
  92.           le32(bsd_header->d_partitions[i].p_offset),
  93.           le32(bsd_header->d_partitions[i].p_size));
  94.       aff_LBA2CHS_rapport(disk_car,le32(bsd_header->d_partitions[i].p_offset));
  95.       ecrit_rapport(" -> ");
  96.       aff_LBA2CHS_rapport(disk_car,le32(bsd_header->d_partitions[i].p_offset)+le32(bsd_header->d_partitions[i].p_size)-1);
  97.       ecrit_rapport("\n");
  98.     }
  99.       }
  100.     }
  101.     if(max_partitions==BSD_MAXPARTITIONS)
  102.       partition->upart_type=UP_FREEBSD;
  103.     else
  104.       partition->upart_type=UP_OPENBSD;
  105.     if(dump_ind!=0)
  106.     {
  107.       dump(stdscr,bsd_header,SECTOR_SIZE);
  108.     }
  109.     return 0;
  110.   }
  111.   return 1;
  112. }
  113.  
  114. int recover_BSD(t_param_disk *disk_car, const struct disklabel*bsd_header,t_diskext *partition,const int debug, const int dump_ind)
  115. {
  116.   int i;
  117.   int i_max_p_offset=0;
  118.   if(test_BSD(disk_car,bsd_header,partition,debug,dump_ind,BSD_MAXPARTITIONS)==0)
  119.   {
  120.     for(i=0;i<BSD_MAXPARTITIONS;i++)
  121.     {
  122.       if(bsd_header->d_partitions[i].p_fstype>0)
  123.       {
  124.     if(le32(bsd_header->d_partitions[i].p_offset)>le32(bsd_header->d_partitions[i_max_p_offset].p_offset))
  125.       i_max_p_offset=i;
  126.       }
  127.     }
  128.     partition->part_size=le32(bsd_header->d_partitions[i_max_p_offset].p_size) +
  129.       le32(bsd_header->d_partitions[i_max_p_offset].p_offset) - partition->lba - 1;
  130.     partition->part_type=P_FREEBSD;
  131.     return 0;
  132.   }
  133.   if(test_BSD(disk_car,bsd_header,partition,debug,dump_ind,OPENBSD_MAXPARTITIONS)==0)
  134.   {
  135.     for(i=0;i<OPENBSD_MAXPARTITIONS;i++)
  136.     {
  137.       if(bsd_header->d_partitions[i].p_fstype>0)
  138.       {
  139.     if(le32(bsd_header->d_partitions[i].p_offset)>le32(bsd_header->d_partitions[i_max_p_offset].p_offset))
  140.       i_max_p_offset=i;
  141.       }
  142.     }
  143.     partition->part_size=le32(bsd_header->d_partitions[i_max_p_offset].p_size) +
  144.       le32(bsd_header->d_partitions[i_max_p_offset].p_offset) - partition->lba - 1;
  145.     partition->part_type=P_OPENBSD;
  146.     return 0;
  147.   }
  148.   return 1;
  149. }
  150.